package com.xrui.hbasecmd.converters;


import com.xrui.hbase.conversion.TypeConverter;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * This class is responsible for converting data from byte[] to long and vice versa.
 */
public class LongConverter extends TypeConverter {

    private static final long serialVersionUID = -7610134214895755716L;

    @Override
    public String toString(byte[] value) {
        if (value == null) {
            return null;
        }
        return Long.toString(Bytes.toLong(value));
    }

    @Override
    public boolean canConvert(byte[] value) {
        try {
            Bytes.toLong(value);
            return true;
        } catch (Exception ignore) {
            return false;
        }
    }

    @Override
    public boolean supportsFormatting() {
        return false;
    }

    @Override
    public byte[] toBytes(String value) {
        if (value == null) {
            return EMPTY_BYTES_ARRAY;
        }
        return Bytes.toBytes(Long.parseLong(value));
    }
}
